layui.use([ 'table', 'form', 'layer', 'laydate', 'laytpl', 'element' ],function() {
	var table = layui.table;
	form = layui.form,layer = layui.layer, 
	$ = layui.jquery,laydate = layui.laydate, 
	laytpl = layui.laytpl,element = layui.element;
	
	/*加载学校类型下拉框*/
	$.ajax({
		type : 'get',
		url : '../json/schoolType.json',
		datatype : 'json',
		success : function(schooltypedate) {
			if (schooltypedate.code == 0) {
				var str = "";
				for(var i = 0; i < schooltypedate.count; i++){
					str += '<option value=' + schooltypedate.data[i].id + '>'+ schooltypedate.data[i].name+'</option>';
				}
				$("#schooltype").append(str);
				$("#schooltypelayer").append(str);
				form.render("select");
			}
		}
	});
	
	/*输入学校编号，失去焦点时把值赋给用户名*/
	$("#schoolid").blur(function(){
		$("#adminuser").val($("#schoolid").val());
	});

	/*页面加载时加载表格*/
	table.render({
		elem : '#tabschool',
		id:'schoolid',
		url : '../sysschoolmanagement/getschoolbywherepage',
		toolbar : '#toolbarDemo',
		title : '学校数据表',
		height:"full-125",
		skin : 'line',
		even : true,
		cols : [ 
			[ {
				type : 'numbers',
				title : '序号',
				align : 'center',
				width : '10%'
			},{
				field : 'schoolid',
				align : 'center',
				title : '学校id',
				width : '30%'
			},{
				field : 'schoolName',
				align : 'center',
				title : '学校名称',
				width : '25%'
			}, {
				field : 'schoolType',
				align : 'center',
				templet : '#schoolTypeText',
				title : '学校类型',
				width : '20%'
			},{
				title : '操作',
				toolbar : '#barDemo',
				align : 'center',
				width : '15%'
			} ]
		],
		page: {
			layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
			groups: 5,
			limit:10,
			theme: '#1E9FFF'
		},
	})
	
	/*点击查询对学校进行筛选*/
	$("#seleBtn").click(function(){
		var schooltype=$("#schooltype").val();
		var schoolname=$("#schoolname").val();
		if(schooltype=="" || schooltype=='undefeated' || schooltype==null || schooltype==00){
			schooltype="";
		}
		table.render({
			elem : '#tabschool',
			id:'schoolid',
			url : '../sysschoolmanagement/getschoolbywherepage?schooltype='+schooltype+"&schoolname="+schoolname,
			toolbar : '#toolbarDemo',
			title : '学校数据表',
			height:"full-125",
			skin : 'line',
			even : true,
			cols : [ 
				[ {
					type : 'numbers',
					title : '序号',
					align : 'center',
					width : '10%'
				},{
					field : 'schoolid',
					align : 'center',
					title : '学校id',
					width : '25%'
				}, {
					field : 'schoolName',
					align : 'center',
					title : '学校名称',
					width : '20%'
				},{
					field : 'schoolType',
					align : 'center',
					title : '学校类型',
					width : '15%'
				},{
					title : '操作',
					toolbar : '#barDemo',
					align : 'center',
					width : '30%'
				} ]
			],
			page: {
				layout: ['prev', 'page', 'next', 'skip', 'count', 'limit'],
				groups: 5,
				limit:10,
				theme: '#1E9FFF'
			},
		})
	});
	
	//添加一个学校
	table.on('toolbar(tabschool)', function(obj) {
		var data = obj.data;
		if(obj.event == 'addSchool'){
				
			var index=layer.open({
				type: 1, 
			    title: '学校信息添加',
		        area: ['830px', '95%'],
		        shade: 0.8,
		        maxmin: true,
		        content: $('#add-schoolLayer'),
		        cancel: function(){ 
		        	$(".add-schoolLayer").css("display","none");   
				}
		    });
	    	layer.full(index);
	    	
	    	$("#addschool").click(function(){
	    		
	    		var index = layer.load(0, {
					shade: 0.1
				});
	    		
				var schoolid = $("#schoolid").val();	
	    		var schooltype = $("#schooltypelayer").val();	
	    		var schoolname = $("#schoolName").val();
	    		var phone = $("#phone").val();

	    		$.ajax({
	    			type : 'get',
	    			url : '../sysschoolmanagement/addschool?schoolid='+schoolid+"&schooltype="+schooltype+"&schoolname="+schoolname + "&phone=" + phone,
	    			datatype : 'json',
	    			success : function(schooltypedate) {
	    				if (schooltypedate.code == 10001) {
	    					layer.closeAll();
	    					table.reload("schoolid", {
	    		                where: {
	    		                	keyword:schooltypedate.code=='10001'
	    		                }
	    		            });
	    					layer.close(index);
	    					layer.alert(schooltypedate.msg, {icon: 1});
	    				}else{
	    					layer.close(index);
	    					layer.alert(schooltypedate.msg, {icon: 2});
	    				}
	    			},
	    			error:function(){
	    				layer.close(index);
	    				layer.alert('出现错误，请重试！', {icon: 2});
	    		    }
	    		});
			})
		}
	});

	//点击修改学校
	table.on('tool(tabschool)', function(obj) {
		var schooldata = obj.data;
		switch (obj.event) {
			case 'editschool':
				$("#inpeditschoolName").val(schooldata.schoolName);
				$("#inpeditschoolid").val(schooldata.schoolid);
				switch(schooldata.schoolType){
					case "10001":
						$("#inpeditschooltype").val("小学");
					break;
					case "10002":
						$("#inpeditschooltype").val("初中");
					break;
					case "10003":
						$("#inpeditschooltype").val("高中");
					break;
					case "10004":
						$("#inpeditschooltype").val("完全小学");
					break;
					case "10005":
						$("#inpeditschooltype").val("完全中学");
					break;
					case "10006":
						$("#inpeditschooltype").val("完全学校");
					break;
				}
				
				//修改学校弹窗
				var index = layer.open({
					type : 1,
					title : '学校信息修改',
					area : [ '440px', '390px' ],
					shade : 0.4,
					content : $('#editschoolLayer'),
				});
				layer.full(index);
				
				$("#editschool").click(function(){
					var schoolid=$("#inpeditschoolid").val();	
					var schoolname=$("#inpeditschoolName").val();	
					$.ajax({
						type : 'get',
						url : '../sysschoolmanagement/editschool?schoolid='+schoolid+"&schoolname="+schoolname,
						datatype : 'json',
						success : function(schooltypedate) {
							if (schooltypedate.code == 10001) {
								layer.confirm(schooltypedate.msg, {
									  btn: ['确定']
								}, function(){
									layer.closeAll();
									table.reload("schoolid", {
						                where: {
						                	keyword:schooltypedate.code=='10001'
						                }
						            });	
								});
							}else{
								layer.alert(schooltypedate.msg, {icon:2});
							}
						},
						error:function(){
							layer.alert('修改失败！', {icon:2});
					    }
					});
				});
			break;
			case 'delschool':
				layer.confirm('确定要删除么？', {
				  btn: ['确定','取消']
				}, function(){
					$.ajax({
		        		type: 'get',
		        		url: "../sysschoolmanagement/deleteschool?schoolid=" + schooldata.schoolid,
		        		dataType: 'json',
		        		success:function(data){
		        			if(data.code == 10001){
		        				layer.alert(data.msg, {icon:1});	
		        				table.reload("schoolid", {
					                where: {
					                	keyword:schooltypedate.code=='10001'
					                }
					            });	
		        			}
		        			else{
		        				layer.alert(data.msg, {icon:2});	
		        			}
		        		},
		        		error:function(){
		        			layer.alert('删除失败！', {icon:2});
		        		},
		        	});
				}, function(){ 
					layer.msg('取消成功！', {icon: 1});
				});
			break;
			
		}
	});

});